<?php
# !!!!!!!!!! DON'T MAKE ANY CHANGE IF YOU DON'T KNOW WHAT YOU'RE DOING !!!!!!!!!!
// --------------------------------------------------------------------------------------------------------------
// File Name              :	classes.php
// Last Modification Time	:	2008-02-01 24:00
// Last Modified by       :	turker (turker.biz@gmail.com)
// list of modifications	:
//
//	- 2008-02-01 turker
//		* 24:00 file created.
//
// --------------------------------------------------------------------------------------------------------------
if (eregi("classes.php",$_SERVER['PHP_SELF'])) {
  header ("Location:../index.php");
  exit;
}

class Mysql {
  var $connection;
  var $debugMode=1;
  var $affectedRows=0;
  var $numQuery=0;
  var $lastId;

  function Mysql() {
    global $DB;
    $this->connection=$this->connect($DB['host'],$DB['username'],$DB['password']);
    $this->selectDb($DB['database']);
    $this->query("SET NAMES 'utf8'");
    $this->query("SET CHARACTER SET 'utf8'");
    $this->query("SET COLLATION_CONNECTION 'utf8_general_ci'");
  }

  function connect($db_server, $db_user, $db_password) {
    $db_connection=mysql_connect($db_server,$db_user,$db_password);
    if(!$db_connection)  {
      $this->mysqlRaiseError();
      die();
      return false;
    }
    else return $db_connection;
  }

  function selectDb($db_database) {
    $db_selection=mysql_select_db($db_database);
    if(!$db_selection) {
      $this->mysqlRaiseError();
      return(0);
      die();
    }
  }

  function query($q) {
    $query=mysql_query($q);
    if ($query) {
      $this->affectedRows=mysql_affected_rows();
      $this->lastId=mysql_insert_id();
      $this->numQuery++;
      return $query;
    }
    else {
      $this->mysqlRaiseError();
      return false;
    }
  }

  function fetch($resultSet,$type = 0) {
    if($type==0) $result=@mysql_fetch_array($resultSet);
    elseif($type==1) $result=mysql_fetch_object($resultSet);
    else $result=mysql_fetch_row($resultSet);
    if ($result) {
      return $result;
    }
    else {
      $this->mysqlRaiseError();
      return false;
    }
  }

  function result($resultSet,$field,$rowId = 0) {
    $return=mysql_result($resultSet,$rowId,$field);
    if($return) return($return);
    else return(false);
  }

  function numCols($resultSet) {
    $cols=mysql_num_fields($resultSet);
    if (!$cols) {
      $this->mysqlRaiseError();
      return false;
    }
    else return $cols;
  }

  function numRows($resultSet) {
    $rows=mysql_num_rows($resultSet);
    if ($rows>0) return $rows;
    else {
      return $this->mysqlRaiseError();
      return false;
    }
  }

  function lastID() {
    return $this->lastId;
  }

  function escape($input) {
    $input=mysql_real_escape_string(trim($input));
    return $input;
  }

  function mysqlRaiseError() {
    if($this->debugMode) {
      return "<center><b><font color='#ff0000' face='tahoma'>.: ".
      "MysqlError: ".mysql_errno($this->connection) . ": " .mysql_error($this->connection).
      " :.</font></b></center>";
    }
  }

  function data_seek($resultSet, $rowNumber = 0) {
    $res=mysql_data_seek($resultSet,$rowNumber);
    if(!$res){
      $this->mysqlRaiseError();
      return false;
    }
    else return $res;
  }

  function free($resultSet) {
    mysql_free_result($resultSet);
  }

  function killConnection() {
    return mysql_close($this->connection);
  }

  function destructor() {
    if($this->connection) $this->killConnection();
  }
}

class Validation {
  var $errorList=array();

  function validation() {
    $this->resetErrors();
  }

  function isEmpty ($value,$msg) {
    $value=trim($value);
    if (empty($value)) {
      $this->errorList[]=$msg;
      return false;
    }
    else return true;
  }

  function isInt($int,$msg) {
    $int=trim($int);
    if (!is_int($int)) {
      $this->errorList[]=$msg;
      return false;
    }
    else return true;
  }

  function isFloat($float,$msg) {
    $float=trim($float);
    if (!is_float($float)) {
      $this->errorList[]=$msg;
      return false;
    }
    else return true;
  }

  function isStr($str,$msg) {
    $str=trim($str);
    if (!is_string($str)) {
      $this->errorList[]=$msg;
      return false;
    }
    else return true;
  }

  function isNumber($value,$msg) {
    $value=trim($value);
    if (!is_numeric($value)) {
      $this->errorList[]=$msg;
      return false;
    }
    else return true;
  }

  function isEqual($input1,$input2,$msg,$type=1) {
    $input1=trim($input1);
    $input2=trim($input2);
    if ($input1==$input2 && $type==1) return true;
    elseif ($input1===$input2 && $type==2) return true;
    else {
      $this->errorList[]=$msg;
      return false;
    }
  }

  function inRange($input,$max,$min,$msg,$type='str') {
    $input=trim($input);
    if ($type=='str' && strlen($input)>=$min && strlen($input)<=$max) return true;
    elseif ($type=='int' && $input>=$min && $input<=$max) return true;
    else {
      $this->errorList[]=$msg;
      return false;
    }
  }

  function checkEmail($email,$msg) {
    $email=trim($email);
    $regexp="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
    if (eregi($regexp,$email)) return true;
    else {
      $this->errorList[]=$msg;
      return false;
    }
  }

  function regExp($input,$regexp,$msg) {
    $input=trim($input);
    if(eregi($regexp,$input)) return true;
    else {
      $this->errorList[]=$msg;
      return false;
    }
  }

  function isError() {
    if (count($this->errorList)>0) return true;
    else return false;
  }

  function getErrors() {
    return $this->errorList;
  }

  function resetErrors() {
    $this->errorList=array();
  }

  function listErrors() {
    foreach ($this->errorList as $error) echo "<br />$error";
  }

  function destructor() {
    $this->errorList=array();
  }
}

class Prepare {

  function prepare() {
    return true;
    setlocale(LC_CTYPE,array('tr','TR','tr_TR','trk','tur','turkey','turkish'));
  }

  function addSlash($input) {
    $input=addslashes(trim($input));
    return $input;
  }

  function delSlash($input) {
    $input=stripslashes(trim($input));
    return $input;
  }

  function html($input) {
    $input=htmlspecialchars(trim($input));
    return $input;
  }

  function lower($input) {
    $f=array('I','İ');
    $r=array('ı','i');
    $input=$this->replace($f,$r,$input);
    $input=mb_strtolower(trim($input),'utf-8');
    return $input;
  }

  function upper($input) {
    $r=array('I','İ');
    $f=array('ı','i');
    $input=$this->replace($f,$r,$input);
    $input=mb_strtoupper(trim($input),'utf-8');
    return $input;
  }

  function upFirst($input) {
    $input=ucfirst(trim($input));
    return $input;
  }

  function upWords($input) {
    $input=ucwords(trim($input));
    return $input;
  }

  function replace($find,$replace,$input) {
    $input=str_replace($find,$replace,trim($input));
    return $input;
  }

  function destructor() {
    return false;
  }
}

class imageUpload {
  var $maxSize=250; # 250KB
  var $maxWidth=400; # in pixel
  var $maxHeight=300; # in pixel
  var $fileExtensions=array('gif','jpg','png','jpeg');
  var $fileTypes=array('image/pjpeg','image/jpeg','image/jpg','image/gif','image/png','image/x-png');
  var $uploadPath='img/big';
  var $thumbSmallPath='img';
  var $thumbMidPath='img/mid';
  var $error=array();
  var $extention;
  var $type;
  var $name;
  var $file;
  var $handlar;

  function imageUpload() {
    if (!is_int($this->maxSize)) $this->maxSize=250;
    if (!is_int($this->maxWidth)) $this->maxWidth=200;
    if (!is_int($this->maxHeight)) $this->maxHeight=350;
    if (!is_array($this->fileExtensions)) $this->fileExtensions=array('gif','jpg','png','jpeg');
    if (!is_array($this->fileTypes)) $this->fileTypes=array('image/pjpeg','image/jpeg','image/jpg','image/gif','image/png','image/x-png');
  }

  function upload($fileArray) {
    # get file informations
    $error=$fileArray['error'];
    $name=$fileArray['name'];
    $type=$fileArray['type'];
    $size=$fileArray['size'];
    $file=$fileArray['tmp_name'];
    $ext=strtolower(end(explode(".", $name)));

    # check file info
    if ($error!=0) { $this->error[]='Hata '.$error; return false; }
    elseif (!in_array($type,$this->fileTypes)) { $this->error[]='Geçersiz tip: '.$type; return false; }
    elseif (!in_array($ext,$this->fileExtensions)) { $this->error[]='Geçersiz uzantı: '.$ext; return false; }
    elseif (!is_uploaded_file($file)) { $this->error[]='Upload edilen dosya değil'; return false; }
    else {
      $this->type=$type;
      $this->extention=$ext;
      $this->name=$name;
      $this->file=$file;
      return true;
    }
  }

  function saveImg($autoThumb=1) {
    if (!$this->haveError()) { // daha önce hata oluşmamış
      $handlar='';
      $new_name=md5($this->name.time());
      switch ($this->type) { // upload edilen dosyayı hafızaya al
      	case 'image/pjpeg':
      	case 'image/jpeg':
      	case 'image/jpg': $handlar=imagecreatefromjpeg($this->file); break;
        case 'image/png':
        case 'image/x-png': $handlar=imagecreatefrompng($this->file); break;
        case 'image/gif': $handlar=imagecreatefromgif($this->file); break;
      }
      $this->handlar=$handlar;

      # find ratio
      $imgX=imagesx($handlar);
      $imgY=imagesy($handlar);
      $ratioX=$ratioY=$ratio=1;
      if ($imgX>$this->maxWidth) $ratioX=round(($imgX/$this->maxWidth),2);
      if ($imgY>$this->maxHeight) $ratioY=round(($imgY/$this->maxHeight),2);
      if ($ratioX>=$ratioY) $ratio=$ratioX;
      else $ratio=$ratioY;
      $imgX=round($imgX/$ratio);
      $imgY=round($imgY/$ratio);

      # save image
      $image=imagecreatetruecolor($this->maxWidth,$this->maxHeight); # kaydedilecek imaj tutucu
      $white=imagecolorallocate($image,255,255,255); // beyaz rengi bul
      imagefill($image,0,0,$white); // imajı beyaz yap
      $dest_x=($this->maxWidth/2)-($imgX/2); // genişliği ortala
      $dest_y=($this->maxHeight/2)-($imgY/2); // yüksekliği ortala
      imagecopyresampled($image,$handlar,$dest_x,$dest_y,0,0,$imgX,$imgY,imagesx($handlar),imagesy($handlar));
      imagejpeg($image,"$this->uploadPath/$new_name.jpg",80); // ana imaji kaydet
      imagedestroy($image);

      if ($autoThumb==1) {
        $this->makeThubms($new_name,80,80,$this->thumbSmallPath);
        $this->makeThubms($new_name,150,150,$this->thumbMidPath);
        imagedestroy($this->handlar);
      }
      imagedestroy($handlar);
      return $new_name.'.jpg';
    }
    else return false;
  }

  function makeThubms($fileName,$tWidth,$tHeight,$path) {
    if (!$this->haveError()) { // daha önce hata oluşmamış
      $handlar=$this->handlar;

      # küçültme oranı bul
      $imgX=imagesx($handlar);
      $imgY=imagesy($handlar);
      $ratioX=$ratioY=$ratio=1;
      if ($imgX>$tWidth) $ratioX=round(($imgX/$tWidth),2);
      if ($imgY>$tHeight) $ratioY=round(($imgY/$tHeight),2);
      if ($ratioX>=$ratioY) $ratio=$ratioX;
      else $ratio=$ratioY;
      $imgX=round($imgX/$ratio);
      $imgY=round($imgY/$ratio);

      # imaj oluştur
      $image=imagecreatetruecolor($tWidth,$tHeight);
      $white=imagecolorallocate($image,255,255,255); // beyaz rengi bul
      imagefill($image,0,0,$white); // imajı beyaz yap
      $dest_x=($tWidth/2)-($imgX/2); // genişliği ortala
      $dest_y=($tHeight/2)-($imgY/2); // yüksekliği ortala
      imagecopyresampled($image,$handlar,$dest_x,$dest_y,0,0,$imgX,$imgY,imagesx($handlar),imagesy($handlar));
      imagejpeg($image,"$path/$fileName.jpg",80);
      imagedestroy($image);

      return true;
    }
    else return false;
  }

  function haveError() {
    if (count($this->error)>0) return true;
    else return false;
  }

  function getErrors() {
    $errors=$this->error;
    return $errors;
  }

  function listErrors() {
    foreach ($this->error as $error) echo "<br />$error";
  }

  function resetErrors() {
    $this->error=array();
  }

  function destructor() {
    $this->error=array();
  }
}

class programHook {
  var $exec=array();
  var $modules=array();

  # initialize the hooks
  function programHook() {
    global $MODULES;
    $this->modules=$MODULES;

    $this->exec['beforeHeader']=array();
    $this->exec['inHeader']=array();
    $this->exec['afterHeader']=array();
    $this->exec['beforeTopMenu']=array();
    $this->exec['inTopMenu']=array();
    $this->exec['afterTopMenu']=array();
    $this->exec['beforeSidebar']=array();
    $this->exec['afterSidebar']=array();
    $this->exec['beforeFooter']=array();
    $this->exec['inFooter']=array();
    $this->exec['afterFooter']=array();
    $this->exec['modulePageStart']=array();
    $this->exec['modulePageBeforePost']=array();
    $this->exec['modulePageAfterPost']=array();
    $this->exec['modulePageEnd']=array();
    $this->exec['beforeLeftMenu']=array();
    $this->exec['afterLeftMenu']=array();
    $this->exec['beforeAdminLeftMenu']=array();
    $this->exec['afterAdminLeftMenu']=array();
    $this->exec['itemAdmin']=array();
    $this->exec['ajax']=array();
    $this->exec['itemAdminLink']=array();
    $this->exec['item']=array();
  }

  function addtoHook($hook,$add) {
   switch ($hook) {
    	case 'beforeHeader': $this->exec['beforeHeader'][]=$add; break;
    	case 'inHeader': $this->exec['inHeader'][]=$add; break;
    	case 'afterHeader': $this->exec['afterHeader'][]=$add; break;
    	case 'beforeTopMenu': $this->exec['beforeTopMenu'][]=$add; break;
    	case 'inTopMenu': $this->exec['inTopMenu'][]=$add; break;
    	case 'afterTopMenu': $this->exec['afterTopMenu'][]=$add; break;
    	case 'beforeSidebar': $this->exec['beforeSidebar'][]=$add; break;
    	case 'afterSidebar': $this->exec['afterSidebar'][]=$add; break;
    	case 'beforeFooter': $this->exec['beforeFooter'][]=$add; break;
    	case 'inFooter': $this->exec['inFooter'][]=$add; break;
    	case 'afterFooter': $this->exec['afterFooter'][]=$add; break;
    	case 'modulePageStart': $this->exec['modulePageStart'][]=$add; break;
    	case 'modulePageBeforePost': $this->exec['modulePageBeforePost'][]=$add; break;
    	case 'modulePageAfterPost': $this->exec['modulePageAfterPost'][]=$add; break;
    	case 'modulePageEnd': $this->exec['modulePageEnd'][]=$add; break;
    	case 'beforeLeftMenu': $this->exec['beforeLeftMenu'][]=$add; break;
    	case 'afterLeftMenu': $this->exec['afterLeftMenu'][]=$add; break;
    	case 'beforeAdminLeftMenu': $this->exec['beforeAdminLeftMenu'][]=$add; break;
    	case 'afterAdminLeftMenu': $this->exec['afterAdminLeftMenu'][]=$add; break;
    	case 'itemAdmin': $this->exec['itemAdmin'][]=$add; break;
    	case 'ajax': $this->exec['ajax'][]=$add; break;
    	case 'itemAdminLink': $this->exec['itemAdminLink'][]=$add; break;
    	case 'item': $this->exec['item'][]=$add; break;
    }
  }

  function loadModules() {
    foreach ($this->modules as $module) {
      $module=strtolower($module);
      if (file_exists("modules/$module.php")) require_once("modules/$module.php");
      else {
        echo "<br />\n$module modülü yüklenemedi.<br />\n";
        die();
      }
    } // foreach
  }

  # define hooks
  function ajax() {
    $exec=$this->exec['ajax'];
    $this->execute($exec);
  }

  function itemAdmin() {
    $exec=$this->exec['itemAdmin'];
    $this->execute($exec);
  }

  function itemAdminLink() {
    $exec=$this->exec['itemAdminLink'];
    $this->execute($exec);
  }

  function item() {
    $exec=$this->exec['item'];
    $this->execute($exec);
  }

  function beforeHeader() {
    $exec=$this->exec['beforeHeader'];
    $this->execute($exec);
  }

  function inHeader() {
    $exec=$this->exec['inHeader'];
    $this->execute($exec);
  }

  function afterHeader() {
    $exec=$this->exec['afterHeader'];
    $this->execute($exec);
  }

  function beforeTopMenu() {
    $exec=$this->exec['beforeTopMenu'];
    $this->execute($exec);
  }

  function inTopMenu() {
    $exec=$this->exec['inTopMenu'];
    $this->execute($exec);
  }

  function afterTopMenu() {
    $exec=$this->exec['afterTopMenu'];
    $this->execute($exec);
  }

  function beforeSidebar() {
    $exec=$this->exec['beforeSidebar'];
    $this->execute($exec);
  }

  function beforeLeftMenu() {
    $exec=$this->exec['beforeLeftMenu'];
    $this->execute($exec);
  }

  function afterLeftMenu() {
    $exec=$this->exec['afterLeftMenu'];
    $this->execute($exec);
  }

  function beforeAdminLeftMenu() {
    $exec=$this->exec['beforeAdminLeftMenu'];
    $this->execute($exec);
  }

  function afterAdminLeftMenu() {
    $exec=$this->exec['afterAdminLeftMenu'];
    $this->execute($exec);
  }

  function afterSidebar() {
    $exec=$this->exec['afterSidebar'];
    $this->execute($exec);
  }

  function beforeFooter() {
    $exec=$this->exec['beforeFooter'];
    $this->execute($exec);
  }

  function inFooter() {
    $exec=$this->exec['inFooter'];
    $this->execute($exec);
  }

  function afterFooter() {
    $exec=$this->exec['afterFooter'];
    $this->execute($exec);
  }

  function modulePageStart() {
    $exec=$this->exec['modulePageStart'];
    $this->execute($exec);
  }

  function modulePageBeforePost() {
    $exec=$this->exec['modulePageBeforePost'];
    $this->execute($exec);
  }

  function modulePageAfterPost() {
    $exec=$this->exec['modulePageAfterPost'];
    $this->execute($exec);
  }

  function modulePageEnd() {
    $exec=$this->exec['modulePageEnd'];
    $this->execute($exec);
  }

  function execute($execs) {
    foreach ($execs as $exec) {
      eval($exec);
    }
  }

  function destructor() {
    $this->exec=array();
    $this->modules=array();
  }
}
?>